// pages/login/index.js
const db = wx.cloud.database()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    account: '', // 账号
    password: '', //密码
    loadModal:false//显示加载框
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  inputaccount: function (e) {
    this.setData({
      account: e.detail.value
    })
  },
  inputpassword: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  login: function () {
    var that = this;
    if (this.data.account == '') {
      wx.showToast({
        title: '请输入账号',
      })
      return;
    }

    if (this.data.password == '') {
      wx.showToast({
        title: '请输入密码',
      })
      return;
    }

    console.log('用户名--->' + this.data.account);
    console.log('网点编码--->' + this.data.password);
    this.showload(true);
    db.collection('es_website').where({
      wdbm: this.data.account,
      mm: this.data.password
    }).get().then(res => {
      console.log(res);
      var result = res.data;
      if (result.length == 0) {
        wx.showToast({
          title: '账号有误',
        })
        that.showload(false)
      } else {
        that.showload(false)
        wx.setStorageSync('account', this.data.account)
        wx.setStorageSync('websitename', result[0].wdmc)
        wx.navigateTo({
          url: '../index/index?level=' + 2,
        })
      }

    }).catch(err => {
      wx.showToast({
        title: '登陆失败',
      })
      that.showload(false)
    })
    
  },
  showload:function(stats){
    this.setData({
      loadModal:stats
    })
  }
})